<template>
  <div class="input-div">
    <el-input
      type="textarea"
      :rows="4"
      placeholder="请输入内容"
      v-model="text"
      maxlength="120"
      show-word-limit
      resize="none"
    ></el-input>
    <el-button-group class="send">
      <el-button id="text-send" plain @click="sendText">发送</el-button>
      <el-button plain icon="el-icon-upload2" @click="uploadFile"></el-button>
    </el-button-group>
  </div>
</template>
<script>
const { dialog } = require("electron").remote;
const crypto = require("crypto");
const path = require("path");
const fs = require("fs");
const net = require("net");
const { Writable } = require("stream");
import { UploadEvent } from "../../smss_socket_event";
import {
  SmsReq,
  MsgHeader,
  MsgType,
  FileUploadReq,
  FileUploadResp,
  FileUploadRes
} from "../../security_msg_pb";
export default {
  name: "InputBox",
  props: ["user"],
  created() {},
  data() {
    return {
      text: "",
      streamClient: null,
      filePath: ""
    };
  },
  methods: {
    sendText() {
      if (this.text !== "" && this.user.id !== 0) {
        fs.readFile(
          "./data/.shadow/pub/" + this.user.id + ".pem",
          (err, data) => {
            if (err) throw err;
            // 使用公钥加密数据
            let smsReq = new SmsReq();
            smsReq.setText(this.text);
            let cipherText = crypto.publicEncrypt(
              {
                key: data.toString(),
                padding: crypto.constants.RSA_PKCS1_PADDING
              },
              smsReq.serializeBinary()
            );
            let msgHeader = new MsgHeader();
            msgHeader.setMsgSize(cipherText.length);
            msgHeader.setMsgType(MsgType.MSG_SEND_REQ);

            msgHeader.setFrom(this.$store.state.User.userID);
            msgHeader.setTo(this.user.id);
            msgHeader.setToken(this.$store.state.User.userToken);
            const headerBuf = msgHeader.serializeBinary();
            let pkgHeader = Buffer.alloc(8);
            pkgHeader.write("AB47");
            pkgHeader.writeInt32LE(headerBuf.length, 4);
            const request = Buffer.concat([pkgHeader, headerBuf, cipherText]);
            this.$socket.write(request, () => {
              this.text = "";
            });
          }
        );
      }
    },
    uploadFile() {
      new UploadEvent(
        this.$store.state.User.userID,
        this.$store.state.User.userToken,
        this.user.id
      );
    },
    onData(data) {
      let flag = data.toString("utf8", 0, 4);
      if (flag !== "AB47") {
        return;
      }
      let headerSize = data.readInt32LE(4);
      let msgHeader = MsgHeader.deserializeBinary(
        data.subarray(8, headerSize + 8)
      );
      let msgType = msgHeader.getMsgType();
      let msgSize = msgHeader.getMsgSize();
      switch (msgType) {
        case MsgType.FILE_UPLOAD_RES:
          const fileUploadRes = FileUploadRes.deserializeBinary(
            data.subarray(8 + headerSize, msgSize + 8 + headerSize)
          );
          if (
            fileUploadRes.getResType() === FileUploadRes.FileUploadResType.OK
          ) {
            this.streamClient.end();
            console.log("上传完成");
          }
          break;
      }
    },
    onFileUploadResp(fileUploadResp) {
      const rr = fs.createReadStream(this.filePath);
      rr.pipe(this.streamClient);
      rr.on("end", () => {
        console.log(">>>>>>>>>>>>结束");
      });
    }
  },
  watch: {}
};
</script>
<style scoped>
.input-div {
  margin-top: 5px;
}
.send {
  margin-top: 5px;
}
#text-send {
  width: 100px;
}
</style>